﻿using System;
using System.Collections.Generic;
using System.Text;

namespace MeTool
{
    public static class DateTimeHelper
    {
        /// <summary>
        /// 返回如下格式的文本信息: yyyy-MM-dd HH:mm
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string ToInfo(this DateTime date)
        {
            return date.ToString("yyyy-MM-dd HH:mm");
        }

        /// <summary>
        /// 返回本 DateTime 的最简文本信息.
        /// <para>当天只返回: HH:mm</para>
        /// <para>昨天返回: 昨天 HH:mm</para>
        /// <para>当月返回: dd日 HH:mm</para>
        /// <para>本年返回: MM-dd HH:mm</para>
        /// <para>其它返回: yyyy-MM-dd HH:mm</para>
        /// </summary>
        /// <param name="date">要计算的日期</param>
        /// <param name="today">设置今天的日期, 默认为系统当前日期</param>
        /// <returns></returns>
        public static string ToSmallInfo(this DateTime date, DateTime? today = null)
        {
            var dToDay = today ?? DateTime.Now;
            if (dToDay.Year != date.Year) return date.ToString("yyyy-MM-dd HH:mm");
            if (dToDay.Month != date.Month) return date.ToString("MM-dd HH:mm");
            if (dToDay.Day == date.Day) return date.ToString("HH:mm");
            if (dToDay.Day == date.AddDays(1).Day) return "昨天 " + date.ToString("HH:mm");
            return date.ToString("dd日 HH:mm");
        }
    }
}
